<?php defined('BASEPATH') OR exit('No direct script access allowed');

class Admin extends Admin_Controller
{
	private $validation_rules = array();

	public function __construct()
	{
		parent::Admin_Controller();
		$this->load->model('galleries/category_m');
		$this->load->model('galleries/gallery_m');
		$this->load->library('form_validation');
		
		$this->lang->load('public');
	}

	public function index()
	{
		$this->data->pagination = create_pagination('admin/galleries/index' , $this->gallery_m->count_all(), 10, 4);
		$this->data->galleries = $this->gallery_m->get_galleries($this->data->pagination['limit']);
		$this->data->categories = $this->category_m->get_many_by(array('parent_id' => 0));
		$this->template
			->build('admin/list_galleries', $this->data);
	}
	public function search()
	{
		$keyword = $this->input->post('keyword');
		$searchtype = $this->input->post('type');
		$category = $this->input->post('category');
		
		$url_searchtype = substr($this->uri->segment(4, 'type_'), 5);
		$url_keyword = url_base64_decode(substr($this->uri->segment(5, 'keyword_'), 8));
		$url_category = substr($this->uri->segment(6, 'category_'), 9);
		
		$keyword = $keyword == ''?$url_keyword: $keyword;
		$searchtype = $searchtype == ''? $url_searchtype: $searchtype;
		$category = $category == ''? $url_category: $category;
		
		if($keyword == '' && 0 == $category)
		{
			redirect('admin/galleries/index', 'refresh');
		}
		
		$pagination = create_pagination(
			'admin/galleries/search/type_' . $searchtype. '/keyword_'. url_base64_encode($keyword). '/category_'. $category. '/' , $this->gallery_m->count_search($keyword , $searchtype, $category), 10, 7);
		$galleries = $this->gallery_m->search($keyword , $searchtype, $category, $pagination['limit']);
		
		$categories = $this->category_m->get_many_by(array('parent_id' => 0));
		
		$this->template
			->set('searchtype', $searchtype)
			->set('keyword', $keyword)
			->set('galleries', $galleries)
			->set('categories', $categories)
			->set('category_selected', $category)
			->set('pagination', $pagination)
			->build('admin/search_galleries', $this->data);
	}

	public function delete_gallery($gallery_id = 0)
	{
		$this->gallery_m->del_gallery($gallery_id);
		$this->session->set_flashdata(array('success'=> '刪除圖片成功'));
		redirect('admin/galleries');
	}
	
	public function handle()
	{
		$this->form_validation->set_rules('child_checkbox[]', '圖片個數', 'required');
		$this->form_validation->set_rules('do', '設置選項', 'required|alpha');
		
		$this->form_validation->set_error_delimiters('', '<br />');
			
		if ($this->form_validation->run() == TRUE)
		{
			$child_checkbox = $this->input->post('child_checkbox');
			$do 			= $this->input->post('do');
			
			foreach($child_checkbox as $key => $gallery_id)
			{
				$gallery_ids[] = $gallery_id;
			}
			switch($do){
				case 'delete':
					$this->gallery_m->delete_galleries_by_ids($gallery_ids);
					break; 
				case 'draft':
					$this->gallery_m->edit_galleries_by_ids($gallery_ids, array('status' => 'draft'));
					break; 
				case 'published':
					$this->gallery_m->edit_galleries_by_ids($gallery_ids, array('status' => 'published'));
					break; 
			}
			
			$this->session->set_flashdata('success', '圖片編輯成功');
			redirect('admin/galleries', 'refresh');
		}
		
		$this->session->set_flashdata('error', validation_errors());
		redirect('admin/galleries', 'refresh');
	}
	
	public function edit_gallery($id = null)
	{
		if ($id == null)
		{
			$id = $this->input->post('id');
		}
		$gallery = $this->gallery_m->get_gallery($id);
		
		if(empty($gallery))
		{
			redirect('admin/galleries/index', 'refresh');
		}
		$this->form_validation->set_rules('title', '圖片標題', 'required|max_length[200]');
		$this->form_validation->set_rules('description', '圖片描述', 'max_length[400]');
		$this->form_validation->set_rules('category', '圖片分類', 'required');
		$this->form_validation->set_rules('status', '圖片狀態', 'required');
		
		if ($this->form_validation->run() == TRUE)
		{
			if (isset($_FILES['userfile']['name']))
			{
				$this->load->library('upload', array(
		        	'upload_path' 		=> $this->settings->get('upload_path'),
		        	'allowed_types' 	=> $this->settings->get('allowed_types'),
		        	'max_size' 			=> $this->settings->get('upload_max_size'),
		        	'max_width'			=> $this->settings->get('upload_max_width'),
		        	'max_height'		=> $this->settings->get('upload_max_height')
		        ));
		        $this->load->language('galleries/public');
		        $this->load->library('galleries/Multi_upload', 'multi_upload');
		        $this->multi_upload->go_edit_upload($gallery);
		    	
		        $data = array('upload_data' => $this->multi_upload->get_success_files(), 'errors' => $this->multi_upload->get_errors());
				if(!empty($data['errors']))
				{
					$this->session->set_flashdata('error', $data['errors'][0]);
					redirect('admin/galleries/edit_gallery/'. $id, 'refresh');
				}
				
			}
	        
			$this->gallery_m->update($id, array(
				'title' => $this->input->post('title'),
				'description' => $this->input->post('description'),
				'votenum' => intval($this->input->post('position')),
				'status' => $this->input->post('status')
			), TRUE);
			
			$category = $this->input->post('category');
			$this->gallery_m->edit_gallery_category($id, intval($category));
			
			$this->session->set_flashdata('success', '編輯圖片成功');
			redirect('admin/galleries/index', 'refresh');
		}
		
		$this->template
			->set('gallery', $gallery)
			->set('categories', $this->category_m->get_many_by(array('parent_id' => 0)))
			->build('admin/edit_gallery');
	}

	public function save_upload_info()
	{
		foreach($_POST as $key => $gallery)
		{
			$info = explode('_', $key);
			$this->gallery_m->update($info[1], array('title' => $gallery[0], 'description' => $gallery[1]), TRUE);
		}
		echo '圖片信息已經更新!';
		exit;
	}
	
	public function categories()
	{
		$categories = $this->category_m->order_by('position', 'ASC')->order_by('id', 'ASC')->get_all();
		$cat_ary = $this->category_m->get_sorted_categories($categories);
		$this->template
			->set('categories', $cat_ary)
			->build('admin/category');
	}
	
	public function create_category()
	{
		$this->form_validation->set_rules('name', '分類名字', 'required|max_length[60]|xss_clean');
		$this->form_validation->set_rules('description', '分類描述', 'required|max_length[200]|xss_clean');
		$this->form_validation->set_rules('parent_id', '父分類', 'numeric|xss_clean');
		$this->form_validation->set_rules('status', '分類狀態', 'required|xss_clean');
		$this->form_validation->set_rules('position', '分類位置', 'max_length[5]|xss_clean');

		$this->form_validation->set_error_delimiters('', '<br />');

		if ($this->form_validation->run() == TRUE)
		{
			$this->category_m->create_category();
			$this->session->set_flashdata('success', '新建分類成功');

			redirect('admin/galleries/create_category', 'refresh');
		}
		$categories = $this->category_m->get_many_by(array('parent_id' => 0));
		$this->template
			->set('categories', $categories)
			->build('admin/create_category');
	}
	
	public function edit_category($id = null)
	{
		if ($id == null)
		{
			$id = $this->input->post('id');
		}
			
		$this->form_validation->set_rules('name', '分類名字', 'required|max_length[60]|xss_clean');
		$this->form_validation->set_rules('description', '分類描述', 'required|max_length[200]|xss_clean');
		$this->form_validation->set_rules('parent_id', '父分類', 'numeric|xss_clean');
		$this->form_validation->set_rules('status', '分類狀態', 'required|xss_clean');
		$this->form_validation->set_rules('position', '分類位置', 'max_length[5]|xss_clean');
		
		$this->form_validation->set_error_delimiters('', '<br />');
			
		$data['category'] = $this->category_m->get_category($id);
		$categories = $this->category_m->get_many_by(array('parent_id' => 0));	
		if ($this->form_validation->run() == TRUE)
		{
			$this->category_m->edit_category($id);
			$this->session->set_flashdata('success', '編輯分類成功');

			redirect('admin/galleries/categories', 'refresh');
		}
		$this->template
			->set('info', $data['category'])
			->set('categories', $categories)
			->build('admin/edit_category');
	}

	public function del_category($id)
	{
		if(0 == intval($id))
		{
			$this->session->set_flashdata('error', lang('invalid_args'));
			redirect('admin/galleries/categories', 'refresh');
		}
		$this->category_m->delete_category($id);
		$this->session->set_flashdata('success', '刪除分類成功');

		redirect('admin/galleries/categories', 'refresh');
	}
	
	public function upload()
	{
		$categories = $this->category_m->get_many_by(array('parent_id' => 0));
		$this->template
			->set('categories', $categories)
			->build('admin/upload');
	}
	
	public function do_upload()
	{
        $this->load->library('upload', array(
        	'upload_path' 		=> $this->settings->get('upload_path'),
        	'allowed_types' 	=> $this->settings->get('allowed_types'),
        	'max_size' 			=> $this->settings->get('upload_max_size'),
        	'max_width'			=> $this->settings->get('upload_max_width'),
        	'max_height'		=> $this->settings->get('upload_max_height')
        ));
        $this->load->language('galleries/public');
        $this->load->library('galleries/Multi_upload', 'multi_upload');
        $this->multi_upload->go_upload();
    	
        $data = array('upload_data' => $this->multi_upload->get_success_files(), 'errors' => $this->multi_upload->get_errors());

        $this->template->set_layout(FALSE)->build('admin/upload_success', $data);
	}
}
?>
